/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.command.PasswordUpdateCommand;
import vip.dexian.admin.command.ProfileUpdateCommand;
import vip.dexian.admin.dto.AdminDTO;
import vip.dexian.admin.dto.AdminPrincipal;
import vip.dexian.admin.enums.GenderEnum;
import vip.dexian.admin.enums.OperateTypeEnum;
import vip.dexian.admin.log.AdminLog;
import vip.dexian.admin.service.AdminService;
import vip.dexian.common.Message;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 个人信息
 *
 * @author 挺好的 2023年06月07日 9:42
 */
@Controller ("adminProfileController")
@RequestMapping ("/admin/profile")
public class ProfileController extends BaseController {

    /**
     * 管理员service
     */
    @Resource (name = "adminServiceImpl")
    private AdminService adminService;

    /**
     * 修改密码
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return modify password view
     */
    @GetMapping ("/modify_password")
    public String modifyPassword (ModelMap model) {
        AdminPrincipal adminPrincipal = this.adminService.getCurrent();
        model.addAttribute("admin", adminPrincipal);
        return "/admin/admin/admin/profile/modify_password";
    }

    /**
     * 修改密码
     *
     * @param command
     *         修改指令
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/modify_password")
    @ResponseBody
    @AdminLog (module = "profile", description = "修改个人密码", type = OperateTypeEnum.UPDATE)
    public Message modifyPassword (PasswordUpdateCommand command) {
        this.adminService.modifyPassword(command);
        return Message.success();
    }

    /**
     * 修改个人信息
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return update view
     */
    @GetMapping ("/update")
    private String update (ModelMap model) {
        AdminDTO admin = this.adminService.findCurrent();
        model.addAttribute("admin", admin);
        model.addAttribute("gender", GenderEnum.values());
        return "/admin/admin/admin/profile/update";
    }

    /**
     * 修改个人信息
     *
     * @param command
     *         修改指令
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/update")
    @ResponseBody
    @AdminLog (module = "profile", description = "修改个人信息", type = OperateTypeEnum.UPDATE)
    public Message update (ProfileUpdateCommand command) {
        this.adminService.modifyProfile(command);
        return Message.success();
    }

    /**
     * 校验原密码是否正确
     *
     * @param oldPassword
     *         原密码
     *
     * @return 原密码是否正确
     */
    @GetMapping ("/check_old_password")
    @ResponseBody
    public boolean isOldPasswordValid (String oldPassword) {
        return !this.adminService.isOldPasswordNonValid(oldPassword);
    }
}
